package com.action;

import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import com.dao.GrantVerifyDAO;
import com.info.UserInfo;
import com.util.GrantVerify;
import com.util.BuildMessages;
import com.util.CommonTools;

/**
 * �û���¼����ز���
 * 
 * @author luanzhe
 * @since 2006-05-15
 * 
 */
public class LoginAction extends Action {

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		DynaActionForm actionForm = (DynaActionForm) form;
		GrantVerifyDAO dao = new GrantVerifyDAO();		
		try {
			if (CommonTools.isNullString((String) actionForm.get("username")) || CommonTools.isNullString((String) actionForm.get("password"))) {
				actionForm.set("username", "");
				actionForm.set("password", "");
				super.saveMessages(request, BuildMessages.build("common.login"));
			} else {
				if (dao.userVerify((String) actionForm.get("username"), (String) actionForm.get("password"))){
					if(dao.userVerifyIP((String) actionForm.get("username"), (String) request.getRemoteAddr())){
						UserInfo info = dao.queryUserInfoByUsername((String) actionForm.get("username"));
						GrantVerify gv = new GrantVerify(dao);
						Map userContainer = gv.getUserContainer(info);
						Locale locale =request.getLocale();
						String language = (String)request.getSession().getAttribute("language");
	
						if(!CommonTools.isNullString(language)){
							if(language.equals("en")){
								userContainer.put("Locale", "en");
							}
							else if(language.equals("zh")){
								userContainer.put("Locale", "zh");
							}
						}
						else{
							userContainer.put("Locale", locale.getLanguage());
						}
						
						//System.out.println("Locale.county="+locale.getCountry()+"/Language="+locale.getLanguage());
						
						request.getSession().setAttribute("userContainer",userContainer);												
						return mapping.findForward("success");
					}else{
						super.saveMessages(request, BuildMessages.build("common.login_ip"));
					}
				}else {					
					super.saveMessages(request, BuildMessages.build("common.login"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null) dao.disConnect();
		}
		return mapping.findForward("login");
	}

	public String getRealURL(String url) {
		int count = 0;
		for (int i = 0; i < url.length(); i++)
			if (url.charAt(i) == '\\' || url.charAt(i) == '/')
				count++;

		String tmppath = "";
		for (int i = 0; i < count - 1; i++)
			tmppath = tmppath + "../";
		if (!tmppath.equals(""))
			tmppath = tmppath.substring(0, tmppath.length() - 1);
		return tmppath + url;
	}
}
